import Vue from "vue"

export const numberToCurrency = (value) => {
    if (!value) return 0
    const intPart = Math.trunc(value)
    const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')
    let floatPart = ''
    const valueArray = value.toString().split('.')
    if (valueArray.length === 2) {
        floatPart = valueArray[1].toString()
        return intPartFormat + '.' + floatPart
    }
    return intPartFormat + floatPart
}

export const numberFormat = (value) => {
    if (!value) return 0
    let money = (value / 10000).toFixed(2)
    return money
}



export const numFilter = (value) => {
    let rep = /[\.]/;
    let realVal = 0;
    if (!isNaN(value) && value !== "" && rep.test(value)) {
        // 截取当前数据到小数点后两位,改变toFixed的值即可截取你想要的数值
        realVal = parseFloat(value).toFixed(2);
    } else {
        realVal = value;
    }
    return realVal;
}


const filters = {
    numberToCurrency,
    numberFormat,
    numFilter
}

Object.keys(filters).forEach(key => {
    Vue.filter(key, filters[key])
})
